package com.example.taximeter.service.impl;

import com.example.taximeter.bean.*;
import com.example.taximeter.config.GlobalParam;
import com.example.taximeter.mapper.CompanyMapper;
import com.example.taximeter.mapper.RoleMapper;
import com.example.taximeter.mapper.UserMapper;
import com.example.taximeter.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户
 */
@Service
public class UserServiceImpl implements UserService {


    private UserMapper userMapper;
    private RoleMapper roleMapper;
    private CompanyMapper companyMapper;

    @Autowired
    public UserServiceImpl(UserMapper userMapper,RoleMapper roleMapper,CompanyMapper companyMapper){
        this.userMapper = userMapper;
        this.roleMapper = roleMapper;
        this.companyMapper = companyMapper;
    }




    @Override
    public ReturnInfo findUserAll(Integer pageNum, Integer pageSize, UserView userView) {
        if(!pageNum.equals(0)||!pageSize.equals(0)){
            PageHelper.startPage(pageNum, pageSize);
        }
        Integer pagetotal = 0;
        Long total = 0L;
        List<UserView> userViews = userMapper.findUserAll(userView);
        PageInfo<UserView> pageInfo = new PageInfo<>(userViews);
        if(userViews.size()!=0){
            pagetotal = pageInfo.getPages();
            total = pageInfo.getTotal();
        }
        return ReturnInfo.info5(pagetotal,total,pageInfo.getList());
    }

    @Override
    public User findUserByUsername(String username) {
        return userMapper.findUserByUsername(username).get(0);
    }

    @Override
    public User findUserByUserid(Integer userid) {
        return userMapper.findUserByUserid(userid);
    }

    @Override
    public UserView findUserViewByUserid(Integer userid) {
        return userMapper.findUserViewByUserid(userid);
    }


    @Override
    public List<User> findUsernameList() {
        return userMapper.findUsernameList();
    }

    @Override
    public List<User> findFullnameList() {
        return userMapper.findFullnameList();
    }

    @Override
    public String addUser(User user) {
       if(userMapper.findUserByUsername(user.getUsername()).size()!=0){
           return "用户名重复";
       }else if(userMapper.addUser(user)){
           return "成功";
       }else {
           return "失败";
       }

    }

    @Override
    public String updateUser(UserView userView) {

        List<User> users = userMapper.findUserByUsername(userView.getUsername());
        if(users.size()==1){
            if(users.get(0).getId()!=userView.getId()){
                return "用户名重复";
            }else {
                //因为是由名称查到id，这里是如果名称不唯一也可以找到一个id
                if(roleMapper.findRoleByRolename(userView.getRolename())!=null&&roleMapper.findRoleByRolename(userView.getRolename()).size()>0){
                    Role role = roleMapper.findRoleByRolename(userView.getRolename()).get(0);
                    userView.setRoleid(role.getRoleid());
                }else {
                    return "失败";
                }
                if(userView.getCompanyname().equals("全部")){
                    userView.setCompanyid(0);
                }else {
                    if(companyMapper.findCompanyByCompanyname(userView.getCompanyname())!=null&&companyMapper.findCompanyByCompanyname(userView.getCompanyname()).size()>0){
                        Company company = companyMapper.findCompanyByCompanyname(userView.getCompanyname()).get(0);
                        userView.setCompanyid(company.getId());
                    }else {
                        return "失败";
                    }
                }
                if(userMapper.updateUser(userView)){
                    return "成功";
                }else {
                    return "失败";
                }
            }
        }else if(users.size()>1){
            return "用户名重复";
        }else {
            //因为是由名称查到id，这里是如果名称不唯一也可以找到一个id
            if(roleMapper.findRoleByRolename(userView.getRolename())!=null&&roleMapper.findRoleByRolename(userView.getRolename()).size()>0){
                Role role = roleMapper.findRoleByRolename(userView.getRolename()).get(0);
                userView.setRoleid(role.getRoleid());
            }else {
                return "失败";
            }
            if(userMapper.updateUser(userView)){
                return "成功";
            }else {
                return "失败";
            }
        }


    }

    @Override
    public String deleteUser(Integer id) {
        if(userMapper.deleteUser(id)){
            return "成功";
        }else {
            return "失败";
        }
    }

    @Override
    public void addUserLog(Integer logtype,String logdescription){
        UserLog userLog = new UserLog();
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        User user1 = userMapper.findUserByUserid(user.getId());
        userLog.setUsername(user1.getUsername());
        userLog.setStime(GlobalParam.getTime());
        userLog.setLogtype(logtype);
        userLog.setLogdescription(logdescription);
        userMapper.addUserLog(userLog);
    }
}
